package cn.hpzy.mzdashboard.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping(path="/mz-dashboard")
public class ParentArrController{

    @Autowired
    JdbcTemplate jdbcTemplate;

    /**
     * 医生实时号源情况
     * @return
     */
    @ResponseBody
    @RequestMapping(path = "/parent", method = RequestMethod.GET)
    public Object arrParentNumSour(){
        //System.out.println("Hello world !!!");
        String sqlString = "select s.dept_name,s.doct_name,s.reglevl_name,sum(s.reg_lmt+s.tel_lmt - s.reged-s.tel_reged) as tel_lmt from  fin_opr_schema s where s.see_date = trunc(sysdate)  and s.valid_flag='1' and (s.reg_lmt+s.tel_lmt-s.reged-s.tel_reged) > 0 and s.end_time >= sysdate  and s.noon_code in ((case when sysdate-to_date(to_char(sysdate,'yyyy-mm-dd')||' 12:00:00','yyyy-mm-dd hh24:mi:ss')<0 then '1' end ),(case when sysdate-to_date(to_char(sysdate,'yyyy-mm-dd')||' 12:00:00','yyyy-mm-dd hh24:mi:ss')>0 then '2' end ),'4') and s.doct_name is not null  and s.dept_code not in ('16001', '16002', '17001', '18001', '19001','19002','19003', '20001', '21001', '21002', '22001') group by s.noon_code,s.dept_name,s.doct_name,s.reglevl_name,s.sroomname,s.dept_code order by s.noon_code,s.dept_code,s.sroomname";
        List<Map<String,Object>> listOpr = jdbcTemplate.queryForList(sqlString);
        String  xmlString = covToXml(listOpr,"arrpnumsour");
        return xmlString;
    }


    /**
     * 医生出诊情况
     * @return
     */
    @ResponseBody
    @RequestMapping(path = "/arrparent")
    public Object arrParent(){
        String sqlString = "select s.dept_name,s.doct_name,t.empl_level from fin_opr_schema s left join (select e.empl_id,(SELECT CONS_INFO_NAME FROM DAWN_CODE_INFO A WHERE A.CONS_TYPE_ID = 'LEVEL' AND A.CONS_INFO_CODE = e.levl_code AND ROWNUM = 1) as empl_level from dawn_org_empl e) t on t.empl_id=s.doct_code where s.see_date=trunc(sysdate+1) and s.valid_flag='1' and s.end_time >= sysdate and s.begin_time <= sysdate   and s.dept_code not in ('16001', '16002', '17001', '18001', '19001','19002', '20001', '21001', '21002') and s.doct_name is not null  group by s.dept_name,s.doct_name,t.empl_level,s.dept_code order by s.dept_code";
        List<Map<String,Object>> listOpr = jdbcTemplate.queryForList(sqlString);
        String xmlString = covToXml(listOpr,"arrp");
        return xmlString;
    }


    /**
     * 封装sql查询结果为xml类型字符
     * @param o
     * @return
     */
    private String covToXml(List<Map<String,Object>> o, String type){
        StringBuffer xmlString = new  StringBuffer();
        for(int i=0; i<o.size(); i++){
            Map<String,Object> obj = o.get(i);
            xmlString.append("<li>");
            if(type.equals("arrpnumsour")){
                //xmlString.append("<span>"+obj.get("NOON_NAME").toString()+"</span>");
                xmlString.append("<span>"+obj.get("DEPT_NAME").toString()+"</span>");
                xmlString.append("<span>"+obj.get("DOCT_NAME").toString()+"</span>");
                xmlString.append("<span>"+obj.get("REGLEVL_NAME").toString()+"</span>");
                if(Double.parseDouble(obj.get("TEL_LMT").toString()) > 10){
                    xmlString.append("<span class=\"text-green\">"+obj.get("TEL_LMT").toString()+"</span>");
                }else if(Double.parseDouble(obj.get("TEL_LMT").toString()) < 5){
                    xmlString.append("<span class=\"text-red \" >"+obj.get("TEL_LMT").toString()+"</span>");
                }else{
                    xmlString.append("<span>"+obj.get("TEL_LMT").toString()+"</span>");
                }
            }else if(type.equals("arrp")){
                xmlString.append("<span>"+obj.get("DEPT_NAME").toString()+"</span>");
                xmlString.append("<span>"+obj.get("DOCT_NAME").toString()+"</span>");
                xmlString.append("<span>"+obj.get("EMPL_LEVEL").toString()+"</span>");
            }

            xmlString.append("</li>\n");
        }
        //System.out.println(xmlString);
        return xmlString.toString();
    }




}